//
//  PKHomeDetailViewController.m
//  PianKe
//
//  Created by 王郑 on 16/2/27.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "PKHomeDetailViewController.h"
#import "UMSocial.h"

@interface PKHomeDetailViewController ()
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation PKHomeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 不允许系统自动修改我们的frame
    self.automaticallyAdjustsScrollViewInsets = NO;
    
#pragma mark --- 自定义导航栏
    [self addBarView];
    
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    _webView.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_webView];
    
    if ([_category isEqualToString:@"TO WATCH"]) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_html5]];
        //把网站内容的二进制转化为一个字符串
        
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        
        
        NSRange range = [str rangeOfString:@"//goAPP"];
        NSRange range2 = [str rangeOfString:@"goApp();"];
        NSRange range3 = NSMakeRange(range.location, range2.length + range2.location - range.location);
        
        
        NSString *str1 = [str stringByReplacingCharactersInRange:range3 withString:@""];
        
        NSRange range4 = [str rangeOfString:@"</section>"];
        NSRange range5 = [str rangeOfString:@"</article>"];
        NSRange range6 = NSMakeRange(range4.location, range5.length + range5.location - range4.location);
        
        NSString *str2 = [str1 stringByReplacingCharactersInRange:range6 withString:@""];
        
        NSRange range7 = [str rangeOfString:@"<!-- article-end -->"];
        NSRange range8 = [str rangeOfString:@"查看更多评论"];
        NSRange range9 = NSMakeRange(range7.location, range8.length + range8.location - range7.location);
        
        NSString *str3 = [str2 stringByReplacingCharactersInRange:range9 withString:@""];
        
       
        [_webView loadHTMLString:str3 baseURL:nil];
    }else {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_html5]];
        //把网站内容的二进制转化为一个字符串
        
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        
        
        NSRange range = [str rangeOfString:@"//goAPP"];
        NSRange range2 = [str rangeOfString:@"goApp();"];
        NSRange range3 = NSMakeRange(range.location, range2.length + range2.location - range.location);
        
        
        NSString *str1 = [str stringByReplacingCharactersInRange:range3 withString:@""];
        
        NSRange range4 = [str rangeOfString:@"</section>"];
        NSRange range5 = [str rangeOfString:@"</article>"];
        NSRange range6 = NSMakeRange(range4.location, range5.length + range5.location - range4.location);
        
        
        
        
        
        NSString *str2 = [str1 stringByReplacingCharactersInRange:range6 withString:@""];
        
        
        [_webView loadHTMLString:str2 baseURL:nil];
        
    }

}

- (void)addBarView
{
    // 创建自定义导航栏
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:headView];
    
    UIButton *leftBack = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBack.frame = CGRectMake(10*kWidthBase, 30*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    [leftBack setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [headView addSubview:leftBack];
    [leftBack addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];

    // 分享按钮
    // 分享功能按钮
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(kScreenWidth - leftBack.width - 20*kWidthBase, leftBack.top, leftBack.width, leftBack.height);
    [shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-gengduo"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [headView addSubview:shareButton];
}

#pragma mark --- 分享 :
- (void)shareAction:(UIButton *)button
{
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_thumbnail]]];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5689e5f467e58e978f00129a"
                                      shareText:[NSString stringWithFormat:@"分享链接 : %@",_html5]
                                     shareImage:image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToWechatTimeline,nil]
                                       delegate:nil];
}






// 实现返回
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
